/**
 * jims
 */
package com.jims.master.modules.doctor.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jims.master.common.utils.DateUtils;
import com.jims.master.modules.doctor.entity.ClinicMaster;
import com.jims.master.modules.doctor.entity.PatMasterIndex;
import com.jims.master.modules.doctor.service.ClinicMasterService;
import com.jims.master.modules.doctor.service.PatMasterIndexService;
import com.jims.master.modules.sys.entity.User;
import com.jims.master.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jims.master.common.data.StringData;
import com.jims.master.common.persistence.Page;
import com.jims.master.common.web.BaseController;
import com.jims.master.common.utils.StringUtils;
import com.jims.master.modules.doctor.entity.PlaceInfectionRecord;
import com.jims.master.modules.doctor.service.PlaceInfectionRecordService;

import java.util.List;

/**
 * Controller
 * @author 系统管理员
 * @version 2017-07-19
 */
@Controller
@RequestMapping(value = "${adminPath}/doctor/placeInfectionRecord")
public class PlaceInfectionRecordController extends BaseController {

	@Autowired
	private PlaceInfectionRecordService placeInfectionRecordService;
	@Autowired
	PatMasterIndexService patMasterIndexService;
	@Autowired
	ClinicMasterService clinicMasterService;
	
	@ModelAttribute
	public PlaceInfectionRecord get(@RequestParam(required=false) String id) {
		PlaceInfectionRecord entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = placeInfectionRecordService.get(id);
		}
		if (entity == null){
			entity = new PlaceInfectionRecord();
		}
		return entity;
	}
	
	@RequiresPermissions("user")
	@RequestMapping(value = {"index", ""})
	public String list(String clinicId, String patientId, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<PlaceInfectionRecord> list=placeInfectionRecordService.findByConditions(clinicId, patientId);
		PatMasterIndex patMasterIndex=patMasterIndexService.get(patientId);
		ClinicMaster clinicMaster=clinicMasterService.get(clinicId);
		model.addAttribute("placeInfection", list);
		model.addAttribute("patMasterIndex", patMasterIndex);
		model.addAttribute("clinicMaster", clinicMaster);
		return "modules/doctor/placeInfectionRecordIndex";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = {"addPlaceInfectionRecord", ""})
	public String addlist(String clinicId, String patientId, HttpServletRequest request, HttpServletResponse response, Model model) {
		PatMasterIndex patMasterIndex=patMasterIndexService.get(patientId);
		User user = UserUtils.getUser();
		ClinicMaster clinicMaster=clinicMasterService.get(clinicId);
		model.addAttribute("user", user);
		String date ="";
		date= DateUtils.getDateTime();
		model.addAttribute("writeDate", DateUtils.parseDate(date));
		model.addAttribute("patMasterIndex", patMasterIndex);
		model.addAttribute("clinicMaster", clinicMaster);
		return "modules/doctor/addPlaceInfectionRecord";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = {"form", ""})
	public String list(String id,HttpServletRequest request, HttpServletResponse response, Model model) {
		PlaceInfectionRecord placeInfectionRecord=placeInfectionRecordService.get(id);
		PatMasterIndex patMasterIndex=patMasterIndexService.get(placeInfectionRecord.getPatientId());
		ClinicMaster clinicMaster=clinicMasterService.get(placeInfectionRecord.getClinicId());
		if(placeInfectionRecord==null){
			String date ="";
			date= DateUtils.getDateTime();
			model.addAttribute("writeDate", DateUtils.parseDate(date));
		}else{
			model.addAttribute("writeDate", placeInfectionRecord.getWriteDate());
		}
		model.addAttribute("placeInfectionRecord", placeInfectionRecord);
		model.addAttribute("patMasterIndex", patMasterIndex);
		model.addAttribute("clinicMaster", clinicMaster);
		return "modules/doctor/placeInfectionRecordForm";
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "save")
	@ResponseBody
	public StringData save(PlaceInfectionRecord placeInfectionRecord, Model model) {
	    StringData data=new StringData();
		int i=placeInfectionRecordService.save(placeInfectionRecord);
        if(i>0){
            data.setCode("success");
            data.setData("保存成功");
        }else{
            data.setCode("error");
            data.setData("保存失败");
        }
        return data;
	}
	@RequiresPermissions("user")
	@RequestMapping(value = "delete")
	@ResponseBody
	public StringData delete(PlaceInfectionRecord placeInfectionRecord) {
	    StringData data=new StringData();
		int i=placeInfectionRecordService.delete(placeInfectionRecord);
        if(i>0){
            data.setCode("success");
            data.setData("删除成功");
        }else{
            data.setCode("error");
            data.setData("删除失败");
        }
		return data;
	}
	/**
	 * 传染病打印打印
	 * @param id
	 * @param clinicId
	 * @param patientId
	 * @return
	 * @author fd
	 */
	@RequiresPermissions("user")
	@RequestMapping(value = {"print", ""})
	@ResponseBody
	public StringData print(String id,String clinicId,String patientId){
		StringData stringData = new StringData();
		String printPath =placeInfectionRecordService.print(id,clinicId, patientId);
		stringData.setCode("success");
		stringData.setData(printPath);
		return stringData;
	}
}